package cn.tedu.sgos.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Miyako
 * @Date: 2024-08-25-13:54
 * @Description: 缓存工具
 */
@Component
public class CacheUtil {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    //持久化存储
    public void set(String key , Serializable value){
        redisTemplate.opsForValue().set(key, value);
    }

    //限时存储
    public void setExpire(String key ,Serializable value , long expire ){
        System.out.println(redisTemplate);
        redisTemplate.opsForValue().set(key,value,expire,TimeUnit.SECONDS);
    }

    //获取redis数据库里的value
    public Serializable get(String key){
        return redisTemplate.opsForValue().get(key);
    }

}
